package cn.ldf.keepaccounts.entity.vo;


/**
 * @author ldf
 * @code May there be no bugs in the world!
 */
@SuppressWarnings("unused")
public class R<T> {

    private int code;
    private String msg;
    private T data;

    public R() {

    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private static <T> R<T> instance(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> condition(boolean flag) {
        return flag ? success("操作成功") : fail("处理失败");
    }

    public static <T> R<T> success(T data) {
        return instance(200, "success", data);
    }

    public static <T> R<T> success(String msg) {
        return instance(200, msg, null);
    }
    public static <T> R<T> data(T data) {
        return instance(200, null, data);
    }

    public static <T> R<T> success() {
        return instance(200, "success", null);
    }

    public static <T> R<T> fail(T data) {
        return instance(500, "fail", data);
    }

    public static <T> R<T> fail(String msg) {
        return instance(500, msg, null);
    }

    public static <T> R<T> fail(int code, String msg) {
        return instance(code == 0 ? 500 : code, msg, null);
    }

    public static <T> R<T> fail() {
        return instance(500, "fail", null);
    }
}
